from matplotlib import lines, transforms
import matplotlib.pyplot as plt
from matplotlib.ticker import (
    MultipleLocator,
    FormatStrFormatter,
    AutoMinorLocator)


def myfunc(x_vals):
    return 0.25 * x_vals - (0.25 * 1.5)


def myfunc2(x_vals):
    if x_vals < 3.5:
        return 0
    return 1


def calc_func(func, vals):
    return list(map(func, vals))


plot_margin = 0.1
semi_black = (0.4, 0.4, 0.4, 1.0)  # RGBA

##########
# figure #
##########
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

#########################
# create visualizations #
#########################
scatter_x = range(0, 8)
scatter_colors = (["#0000FF"] * 4) + (["#FF0000"] * 4)

line_xs = [0.5, 6.5]
line = lines.Line2D(
    line_xs,
    calc_func(myfunc, line_xs),
    linewidth=1.4,
    color=semi_black)
line.set_clip_on(False)

########
# plot #
########
ax.scatter(
    scatter_x,
    calc_func(myfunc2, scatter_x),
    s=None,
    c=scatter_colors,
    marker="x")
ax.add_line(line)
ax.axhline(0.5, linestyle="--")
ax.axvline(3.5, linestyle="--")

##########
# limits #
##########
ax.set_xlim((-2, 8))
ax.set_ylim((-0.3 - plot_margin, 1 + plot_margin))

##############
# axis ticks #
##############
ax.set_yticks([0, 0.5, 1])
ax.set_xticks([0, 1, 2, 3, 4, 5, 6, 7])
ax.yaxis.set_minor_locator(MultipleLocator(0.1))
ax.xaxis.set_minor_locator(MultipleLocator(0.25))
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')

# # Create offset transform by 5 points in x direction
# matplotlib_dpi = 72
# dx = 0 / matplotlib_dpi
# dy = 100 / matplotlib_dpi
# offset = transforms.ScaledTranslation(dx, dy, fig.dpi_scale_trans)

# # apply offset transform to all y ticklabels.
# for label in ax.yaxis.get_majorticklabels():
#     label.set_transform(label.get_transform() + offset)

# ax.yaxis.set_label_coords(0, 0, transform=offset)


ax.tick_params(
    axis="y",

    # ticks
    # top=False,
    # bottom=False,
    # left=True,
    # right=False,
    # labels
    # labelleft=True,
    labelbottom=False)

##########
# spines #
##########
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_position('zero')
ax.spines['bottom'].set_position('zero')

########
# grid #
########
# ax.grid(
#     which="major",
#     axis="both",
#     color="black",
#     alpha=0.18,
#     linestyle="--",
#     linewidth=0.75)

# ax.grid(
#     which="minor",
#     axis="both",
#     color="black",
#     alpha=0.12,
#     linestyle="--",
#     linewidth=0.6)

##########
# export #
##########
fig.savefig("plot.pdf")
fig.savefig("plot.png")
fig.savefig("plot.svg")
